package com.iflytek.crvoice2txt.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

public abstract class CommonListViewAdapter<T> extends BaseAdapter{

    protected LayoutInflater mLayoutInflater;
    protected Context mContext;
    protected List<T> mData;
    protected int mItemLayoutId;

    private OnCommonListViewItemClick onCommonListViewItemClick;

    public interface OnCommonListViewItemClick{
        void onCommonListViewItemClick(int position);
    }

    public void setOnCommonListViewItemClick(OnCommonListViewItemClick onCommonListViewItemClick) {
        this.onCommonListViewItemClick = onCommonListViewItemClick;
    }

    public CommonListViewAdapter(Context mContext, List<T> mData, int mItemLayoutId) {
        mLayoutInflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
        this.mData = mData;
        this.mItemLayoutId = mItemLayoutId;
    }


    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public T getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        //实例化一个ViewHolder
        ListViewHolder holder = getViewHolder(position, convertView, parent);
        //使用对外公开的convert方法，通过ViewHolder把View找到，通过Item设置值
        convert(holder, getItem(position));
        if(onCommonListViewItemClick!=null) {
            holder.getmConvertView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   onCommonListViewItemClick.onCommonListViewItemClick(position);
                }
            });
        }
        return holder.getmConvertView();
    }

    private ListViewHolder getViewHolder(int position, View convertView, ViewGroup parent){
        return ListViewHolder.getHolder(mContext, mItemLayoutId, convertView, parent);
    }

    /**
     * 对外公布了一个convert方法，并且还把ViewHolder和本item对应的Bean对象给传出去
     * 现在convert方法里面需要干嘛呢？通过ViewHolder把View找到，通过Item设置值
     */
    public abstract void convert(ListViewHolder holder, T item);

}
